/**
 * 
 */
package com.hanhai.zrb.api.biz.service.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.hanhai.zrb.model.fund.Fund;
import com.hanhai.zrb.model.intention.Intention;
import com.hanhai.zrb.model.message.enums.MessageBizType;
import com.hanhai.zrb.model.project.Project;
import com.hanhai.zrb.model.user.User;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * @author leiruiqi@hanhai.com
 *
 */
@Service("messageBizContentCreateService")
public class MessageBizContentCreateService {

	public String buildContent(MessageBizType bizType,Map<String,Object> data){
		String str = bizType.getContentTemplate();
		return freemarkerProcess(data,str);
	}
	
	
	public String getAttentionUserMsg(User user){
		String str = MessageBizType.attention_user.getContentTemplate();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("realname", user.getRealname());
		data.put("action", "user:"+user.getUid());
		return freemarkerProcess(data,str);
	}
	
	public String getAttentionDynamicChoiceMsg(User user,Object model){
		String str = MessageBizType.attention_dynamic_choice.getContentTemplate();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("realname", user.getRealname());
		if(model instanceof Project){
			Project project = (Project)model;
			data.put("title", project.getProjectName());
			data.put("action", "project:"+project.getId());
			
		}
		else if(model instanceof Fund){
			Fund fund = (Fund)model;
			data.put("title", fund.getTitlesVal());
			data.put("action", "fund:"+ fund.getId());
		}
		else{
			throw new RuntimeException();
		}
		return freemarkerProcess(data,str);
	}
	
	public String getAttentionDynamicIntentionMsg(User user,Intention intention){
		String str = MessageBizType.attention_dynamic_intention.getContentTemplate();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("realname", user.getRealname());
		data.put("title", intention.getIntro());

		return freemarkerProcess(data,str);
	}
	
	public String getCollectionChoiceMsg(User user,Object model){
		String str = MessageBizType.colleciton_choice.getContentTemplate();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("realname", user.getRealname());
		data.put("action", "user:"+user.getUid());
		if(model instanceof Project){
			Project project = (Project)model;
			data.put("title", project.getProjectName());
			
		}
		else if(model instanceof Fund){
			Fund fund = (Fund)model;
			data.put("title", fund.getTitlesVal());
		}
		else{
			throw new RuntimeException();
		}

		return freemarkerProcess(data,str);
	}
	
	public String getChoicePublishMsg(Object model){
		String str = MessageBizType.choice_publish.getContentTemplate();
		Map<String,Object> data = new HashMap<String,Object>();
		if(model instanceof Project){
			Project project = (Project)model;
			data.put("title", project.getProjectName());
			data.put("action", "project:"+project.getId());
			
		}
		else if(model instanceof Fund){
			Fund fund = (Fund)model;
			data.put("title", fund.getTitlesVal());
			data.put("action", "fund:"+ fund.getId());
		}
		else{
			throw new RuntimeException();
		}

		return freemarkerProcess(data,str);
	}
	
	public static String freemarkerProcess(Map input, String templateStr) {
		StringTemplateLoader stringLoader = new StringTemplateLoader();
		String template = "content";
		stringLoader.putTemplate(template, templateStr);
		Configuration cfg = new Configuration();
		cfg.setTemplateLoader(stringLoader);
		try {
			Template templateCon = cfg.getTemplate(template);
			StringWriter writer = new StringWriter();
			templateCon.process(input, writer);
			return writer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
		return null;
	}

	// test
	public static void main(String[] args) {
		String template = "你好${姓名!'未知'}，今天是${date?string('yyyy-MM-dd')}"; //变量参考freemarker语法
		Map m = new HashMap();
		m.put("姓名", "管理员");
		m.put("date", new Date());
		System.out.println(freemarkerProcess(m, template)); //"你好管理员，今天是2013-09-11"
		
		Map m2 = new HashMap();
		m2.put("realname", "雷瑞奇");
		m2.put("jump", "user:100000055");
		String str = MessageBizType.attention_user.getContentTemplate();
		System.out.println(freemarkerProcess(m2,str));
	}

}
